<?php
final class ProcessLock{
    private $handle;

    public function lock($key){
        $file=DIR_SYSTEM . 'process_lock/' . $this->mycrc32(preg_replace('/[^A-Z0-9\._-]/i', '', $key));

        $this->handle=fopen($file, 'w+');

        if(flock($this->handle, LOCK_EX | LOCK_NB)){
            return TRUE;
        }else{
            fclose($this->handle);
            return FALSE;
        }
    }
    
    public function unlock(){
        flock($this->handle, LOCK_UN);
        fclose($this->handle);
    }
    
    private function mycrc32($key){
        $crc = abs (crc32 ($key));

        if ($crc & 0x80000000){
            $crc ^= 0xffffffff;
            $crc += 1;
        }

        return $crc;
	  }
}
?>